package config

import (
	"github.com/spf13/viper"
)

type MQTTConfig struct {
	Broker   string
	ClientID string
	Username string
	Password string
}

type KafkaConfig struct {
	Brokers []string
	Topic   string
	GroupID string // Add this field
}

type Config struct {
	Postgres PostgresConfig
	InfluxDB InfluxDBConfig
	MQTT     MQTTConfig
	Kafka    KafkaConfig
}

type PostgresConfig struct {
	User     string
	Password string
	Host     string
	Port     int
	DBName   string
	SSLMode  string
}

type InfluxDBConfig struct {
	URL string
	Org string
}

func LoadConfig() (*Config, error) {
	viper.SetConfigFile("config/config.yaml")
	if err := viper.ReadInConfig(); err != nil {
		return nil, err
	}

	cfg := &Config{
		Postgres: PostgresConfig{
			User:     viper.GetString("postgres.user"),
			Password: viper.GetString("postgres.password"),
			Host:     viper.GetString("postgres.host"),
			Port:     viper.GetInt("postgres.port"),
			DBName:   viper.GetString("postgres.dbname"),
			SSLMode:  viper.GetString("postgres.sslmode"),
		},
		InfluxDB: InfluxDBConfig{
			URL: viper.GetString("influxdb.url"),
			Org: viper.GetString("influxdb.org"),
		},
		MQTT: MQTTConfig{
			Broker:   viper.GetString("mqtt.broker"),
			ClientID: viper.GetString("mqtt.client_id"),
			Username: viper.GetString("mqtt.username"),
			Password: viper.GetString("mqtt.password"),
		},
		Kafka: KafkaConfig{
			Brokers: viper.GetStringSlice("kafka.brokers"),
			Topic:   viper.GetString("kafka.topic"),
			GroupID: viper.GetString("kafka.group_id"), // Add this line
		},
	}

	return cfg, nil
}
